package db.swing2;

import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

import db.swing2.Controller.MyHitListSelectionListener;
import db.swing2.Controller.MyWindowClosingListener;
import db.swing2.Controller.SearchNameActionListener;
import de.dnb.basics.Misc;
import de.dnb.basics.applicationComponents.RangeCheckUtils;
import de.dnb.gnd.parser.Format;
import de.dnb.gnd.parser.Record;
import de.dnb.gnd.utils.GNDUtils;
import de.dnb.gnd.utils.RecordUtils;

public final class View {

	public View() {
		super();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					gui = new GUI();
					gui.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private GUI gui;

	public String getSelectedRadioButton() {
		Enumeration<AbstractButton> enumeration = gui.buttonGroupType
				.getElements();
		while (enumeration.hasMoreElements()) {
			AbstractButton button = enumeration.nextElement();
			if (button.isSelected())
				return button.getText();
		}
		return null;
	}

	public String getSearchTerm() {
		return gui.textFieldSearchTerm.getText();
	}

	public String getSearchIDN() {
		return gui.textFieldIDN.getText();
	}

	public int getSelectedHitListRow() {
		return gui.tableHitList.getSelectedRow();
	}

	public void setRecord(Record record) {
		RangeCheckUtils.assertReferenceParamNotNull("record", record);
		String name = GNDUtils.getNameOfRecord(record);
		gui.labelRecordName.setText(name);
		// Zwischen den <nobr>-Tags erfolgt kein Umbruch:
		String recordText = RecordUtils.toPica(record, Format.PICA3, true,
				"</nobr><br><nobr>", '$');
		recordText = recordText.replace("@", "&#64;");
		recordText = Misc.convertLinksToHyperlinks(recordText);
		final String text = "<h3>IDN: " + record.getId() + "</h3><nobr>"
				+ recordText + "</nobr>";

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				gui.editorPaneRecord.setText(text);
			}
		});
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				gui.scrollPaneRecord.getVerticalScrollBar().setValue(0);
			}
		});
	}

	public void setLinkListener(HyperlinkListener listener) {
		gui.editorPaneRecord.addHyperlinkListener(listener);
	}

	public void setStatus(final String text) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.labelStatus.setText(text);
			}
		});

	}

	public final void setTreeSelectionListener(
			final TreeSelectionListener listener) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.treeUBB.addTreeSelectionListener(listener);
			}
		});
	}

	public void setTree(final OBUBTreeNode rootNode) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.labelRootName.setText(rootNode.toString());
				gui.treeUBB.setModel((new DefaultTreeModel(rootNode)));
				gui.scrollPaneTreeUBB.getVerticalScrollBar().getModel()
						.setValue(0);
				gui.scrollPaneTreeUBB.getHorizontalScrollBar().getModel()
						.setValue(0);
			}
		});
	}

	public void setSearchIDActionListener(ActionListener listener) {
		gui.btnIdnSearchID.addActionListener(listener);
		gui.textFieldIDN.addActionListener(listener);
	}

	public void setSearchNameActionListener(SearchNameActionListener listener) {
		gui.btnSearchName.addActionListener(listener);
		gui.textFieldSearchTerm.addActionListener(listener);
		Enumeration<AbstractButton> enumeration = gui.buttonGroupType
				.getElements();
		while (enumeration.hasMoreElements()) {
			AbstractButton button = enumeration.nextElement();
			button.addActionListener(listener);
		}

	}

	public void close() {
		gui.setVisible(false);
		gui.dispose();
	}

	public void addWindowClosingListener(
			MyWindowClosingListener myWindowListener) {
		gui.addWindowListener(myWindowListener);
	}

	public void setHitListSelectionListener(MyHitListSelectionListener listener) {
		gui.tableHitList.getSelectionModel().addListSelectionListener(listener);

	}

	public void setHitListModel(final HitListTableModel model) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if (model == null)
					setHitCount(0);
				else
					setHitCount(model.getRowCount());
				gui.tableHitList.setModel(model);
				TableColumnModel colMod = gui.tableHitList.getColumnModel();
				colMod.getColumn(0).setMaxWidth(5);
				colMod.getColumn(1).setMaxWidth(5);
				colMod.getColumn(3).setMaxWidth(70);
				gui.scrollPaneHilList.getVerticalScrollBar().getModel()
						.setValue(0);
			}
		});
	}

	public void setHitCount(final int hitCount) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				gui.lblHitCount.setText(Integer.toString(hitCount));
			}
		});

	}

	public HitListTableModel getHitListTableModel() {
		return (HitListTableModel) gui.tableHitList.getModel();
	}

	public void addError(final String error) {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				String errors = gui.textPaneErrors.getText();
				errors += "\n" + error;
				gui.textPaneErrors.setText(errors);
			}
		});

	}
}
